package com.yonyou.cn.mongodbtest.utils;

import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TestDemoMongodb {

	public static void main(String[] args) {
		List<String> ips =new ArrayList<String>();
		ips.add("192.168.50.123");
		ips.add("192.168.50.124");
		ips.add("192.168.50.125");
		String username = "";
		String password = "";
		//getCollection(String ip, int port, String username, String password, String dbname, String table) {
		MongoCollection<Document> conn = MongoDBUtils.getDBConnection("config", "test_test", ips, 27017,username,password);
		/**
		 * 给表建索引,1升序，-1降序
		 */
		 //conn.createIndex(new BasicDBObject().append("account", 1).append("ac_id", 1));

     
		/**
		 * 批量更新操作,数据有批量更新，没有插入操作
		 */
		/*List<WriteModel<Document>> requests = new ArrayList<WriteModel<Document>>();
		long l = System.currentTimeMillis();
		for (int i = 1; i <= 100000; i++) {
			//更新条件
			Document queryDocument = new Document("id",i);
			//更新内容，改下书的价格
			Document updateDocument = new Document("$set", new Document("age", 104).append("sex", "女"));
			//构造更新单个文档的操作模型
			UpdateOneModel<Document> uom = new UpdateOneModel<Document>(queryDocument,updateDocument,new UpdateOptions().upsert(true));
			//UpdateOptions代表批量更新操作未匹配到查询条件时的动作，默认false，什么都不干，true时表示将一个新的Document插入数据库，他是查询部分和更新部分的结合
			requests.add(uom);
		}
		BulkWriteResult bulkWriteResult = conn.bulkWrite(requests);
		long l2 = System.currentTimeMillis();
		System.out.println("耗时====>"+(l2-l));
		System.out.println(bulkWriteResult.toString());
		*/
	    /**
	     * 批量插入操作
	     */
	    List<Document> list = new ArrayList<>();
	    long l = System.currentTimeMillis();
	    for(int i = 0; i <= 3; i++) {
	       Document document = new Document("_id", i)
	                .append("DIST_CODE", "1800")
	                .append("DIST_NAME", "五华区")
	                .append("SSAGENCY_CODE", "17001")
	                .append("SSAGENCY_NAME", "九州财政局")
	                .append("INS_TYPE_CODE", "1105")
	                .append("PAY_PER", "201907")
	                .append("COR_PAY_PER", "201907")
	                .append("BANK_CODE", "1200")
	                .append("BANK_NAME", "中国银行")
	                .append("BANK_ACC_NO", "123456894")
	                .append("BANK_ACC_NAME", "银川财政专户")
	                .append("AMOUNT", 100+i)
	                .append("MAINBODY_TYPE", "1")
	                .append("MAINBODY_CODE", "aac001")
	                .append("MAINBODY_NO", "41152115678921")
	                .append("MAINBODY_NAME", "李宇")
	                .append("PAY_TIME", "20190826")
	                .append("IO_ITEM_CODE", "5105")
	                .append("IO_ITEM_NAME", "生育支出")
	                .append("PAY_TYPE", "1")
	                .append("INS_TYPE_NAME", "职工生育保险")
	                .append("APPLY_ID", "123")
	                .append("RECDETAIL_ID", "123")
	                .append("YEAR", "2019")
	                .append("MONTH", "07")
	                .append("CLT_LEVEL", "0");
	        list.add(document);
	    }
	    String [] aa = "SSSFM_YBZC_DETAIL@YEAR@MONTH".split("@");
	    BasicDBObject hh = new BasicDBObject();
	    for (int i = 0; i < aa.length; i++) {
			if(i != 0) {
				hh.append(aa[i], "$"+aa[i]);
			}
		}
	    BasicDBObject groupFields = new BasicDBObject("_id", hh);
	    BasicDBObject group = new BasicDBObject("$group", groupFields);
	    List<BasicDBObject> bsons = new ArrayList<BasicDBObject>();
	    bsons.add(group);
	    BasicDBObject group1 = new BasicDBObject("YEAR", "2019").append("MONTH", "07");
	    MongoCursor<Document> find = conn.find(group1).iterator();
	    MongoCursor<Document> cursor = conn.aggregate(bsons).iterator();
		//conn.insertMany(list);
	    //MongoCursor<Document> cursor = conn.aggregate(pipeline).iterator();
	    while (find.hasNext()) {
	        System.out.println(find.next());
	        System.out.println("++++++++");
	    }
	    while (cursor.hasNext()) {
	    	Map<String, String> map = (Map<String, String>) cursor.next().get("_id");
	        System.out.println(map.get("YEAR")+":"+map.get("MONTH"));
	    }
		//conn.drop();
	    long l2 = System.currentTimeMillis();
		System.out.println("耗时====>"+(l2-l)/60000+"分钟");
		//System.err.println("====>"+conn.count());
		
		/**
		 * 游标遍历
		 */
		/*FindIterable findIterable = conn.find();
	    MongoCursor cursor = findIterable.iterator();
	    long l = System.currentTimeMillis();
	    int count=0;
	    while (cursor.hasNext()) {
	        System.out.println(cursor.next());
	        count+=1;
	    }
	    System.out.println("====>count="+count);
	    long l2 = System.currentTimeMillis();
		System.out.println("耗时====>"+(l2-l));*/
		
		
		
	}
}


